package cn.org.seventh.tools.plugin;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;

import com.thoughtworks.qdox.model.JavaClass;

/**
 * ibatis SqlMapConfig.xml�����ļ���ɲ��</br> ˵��</br>
 * ͨ��ָ��һ���SqlMapConfig.xml�������ļ�������</br>
 * �ֹ����õ�&lt;sqlMapConfig/&gt;�ڵ��ڵ����ݺϲ����</br>
 * ��Ŀ�������ļ���,�ò������ʱָ����Ҫ��ɵ�ԴĿ¼�� �÷�:</br>
 * �μ�xdoclet2��ant��maven����˵��
 * 
 * @author <a href="mailto:seventh@126.com">liji</a>
 * @since 2008-12-22 ����02:06:30
 */
public class IbatisConfigPlugin extends QDoxPlugin {

	private String baseConfig;

	public IbatisConfigPlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public IbatisConfigPlugin(FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public String getBaseConfig() {
		String content = null;
		if (baseConfig == null || "".equals(baseConfig)) {
			throw new RuntimeException("base config must be define!");
		}
		try {
			baseConfig = baseConfig.replace('\\', '/');
			URL url = getClass().getResource(
					baseConfig.startsWith("/") ? baseConfig : "/" + baseConfig);
			File file = null;
			if (null != url) {
				file = new File(url.getFile());
			} else {
				file = new File(baseConfig);
			}
			// SAXReader reader = new SAXReader();
			// Document document = reader.read(file);
			// Element root = document.getRootElement();
			// ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
			// OutputFormat format = OutputFormat.createCompactFormat();
			// format.setSuppressDeclaration(true);
			// format.setEncoding("UTF-8");
			// format.setNewlines(true);
			// format.setIndent(true);
			// XMLWriter writer = new XMLWriter(out, format);
			// for (int i = 0; i < root.content().size(); i++) {
			// Node node = (Node) root.content().get(i);
			// writer.write(node);
			// }
			// content = new String(out.toByteArray(), "UTF-8");
			// writer.flush();
			// writer.close();
			// BufferedReader in = new BufferedReader(new InputStreamReader(new
			// FileInputStream(file)));
			FileInputStream f = new FileInputStream(file);
			ByteArrayOutputStream o = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int i = 0;
			StringBuffer s = new StringBuffer("");
			while ((i = f.read(b)) != -1) {
				o.write(b, 0, i);
				s.append(new String(b));
			}
			content = s.toString().split("\\<sqlMapConfig\\>")[1]
					.split("\\</sqlMapConfig\\>")[0];
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return content;
	}

	/**
	 * 
	 * @param baseConfig
	 */
	public void setBaseConfig(String baseConfig) {
		this.baseConfig = baseConfig;
	}

	public String getSqlMap(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getFullyQualifiedName().replace('.', '/') + ".xml";
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(IbatisConfigTagImpl.NAME) == null ? false
				: true;
	}
}
